﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using MediaMonitor.Common;
using MediaMonitor.Controls;
using Telerik.Web.UI;
using System.Web.Services;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections;
using System.IO;


namespace MediaMonitor.Users
{
    public partial class upload_templates : System.Web.UI.Page
    {
        string filename1, filename2, filename3, filename4, filename5, filename6;
        Template temp = new Template();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["UserID"] != null)
            {
                BindData();

            }
            else
                Response.Redirect(Utility.AppPath + "/logout.aspx");

        }
        protected void imgBtnUpload_Click(object sender, ImageClickEventArgs e)
        {
            if (temp.Id == 0)
            {
                if (fleTemplate1.HasFile)
                {
                    filename1 = Session["UserID"].ToString() + "Template 1.ppt";
                    fleTemplate1.SaveAs(Server.MapPath("~/Templates/") + filename1);
                }
                else
                    filename1 = "Template 1.ppt";
                if (fleTemplate2.HasFile)
                {
                    filename2 = Session["UserID"].ToString() + "Template 2.ppt";
                    fleTemplate2.SaveAs(Server.MapPath("~/Templates/") + filename2);
                }
                else
                    filename2 = "Template 2.ppt";
                if (fleTemplate3.HasFile)
                {
                    filename3 = Session["UserID"].ToString() + "Template 3.ppt";
                    fleTemplate3.SaveAs(Server.MapPath("~/Templates/") + filename3);
                }
                else
                    filename3 = "Template 3.ppt";
                if (fleTemplate4.HasFile)
                {
                    filename4 = Session["UserID"].ToString() + "Template 4.ppt";
                    fleTemplate4.SaveAs(Server.MapPath("~/Templates/") + filename4);
                }
                else
                    filename4 = "Template 4.ppt";
                if (fleTemplate5.HasFile)
                {
                    filename5 = Session["UserID"].ToString() + "Template 5.ppt";
                    fleTemplate5.SaveAs(Server.MapPath("~/Templates/") + filename5);
                }
                else
                    filename5 = "Template 5.ppt";
                if (fleTemplate6.HasFile)
                {
                    filename6 = Session["UserID"].ToString() + "Template 6.ppt";
                    fleTemplate6.SaveAs(Server.MapPath("~/Templates/") + filename6);
                }
                else
                    filename6 = "Template 6.ppt";
                if (FileUploadIconTVBig.HasFile)
                {
                    temp.TvBig = Session["UserID"].ToString() + "TV_Big.png";
                    FileUploadIconTVBig.SaveAs(Server.MapPath("~/Templates/") + temp.TvBig);
                }
                else
                    temp.TvBig = "TV_Big.png";
                if (FileUploadIconTVSmall.HasFile)
                {
                    temp.TvSmall = Session["UserID"].ToString() + "TV_Small.png";
                    FileUploadIconTVSmall.SaveAs(Server.MapPath("~/Templates/") + temp.TvSmall);
                }
                else
                    temp.TvSmall = "TV_Small.png";
                if (FileUploadIconCableBig.HasFile)
                {
                    temp.CableBig = Session["UserID"].ToString() + "Cable_Big.png";
                    FileUploadIconCableBig.SaveAs(Server.MapPath("~/Templates/") + temp.CableBig);
                }
                else
                    temp.CableBig = "Cable_Big.png";
                if (FileUploadIconCableSmall.HasFile)
                {
                    temp.CableSmall = Session["UserID"].ToString() + "Cable_small.png";
                    FileUploadIconCableSmall.SaveAs(Server.MapPath("~/Templates/") + temp.CableSmall);
                }
                else
                    temp.CableSmall = "Cable_small.png";
                if (FileUploadIconInternetBig.HasFile)
                {
                    temp.InternetBig = Session["UserID"].ToString() + "Internet_big.png";
                    FileUploadIconInternetBig.SaveAs(Server.MapPath("~/Templates/") + temp.InternetBig);
                }
                else
                    temp.InternetBig = "Internet_big.png";
                if (FileUploadIconInternetSmall.HasFile)
                {
                    temp.InternetSmall = Session["UserID"].ToString() + "Internet_small.png";
                    FileUploadIconInternetSmall.SaveAs(Server.MapPath("~/Templates/") + temp.InternetSmall);
                }
                else
                    temp.InternetSmall = "Internet_small.png";
                if (FileUploadIconOutdoorBig.HasFile)
                {
                    temp.OutdoorBig = Session["UserID"].ToString() + "Outdoor_Big.png";
                    FileUploadIconOutdoorBig.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorBig);
                }
                else
                    temp.OutdoorBig = "Outdoor_Big.png";
                if (FileUploadIconOutdoorIntBig.HasFile)
                {
                    temp.OutdoorIntBig = Session["UserID"].ToString() + "Ootdoor_Int_Big.png";
                    FileUploadIconOutdoorIntBig.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorIntBig);
                }
                else
                    temp.OutdoorIntBig = "Ootdoor_Int_Big.png";
                if (FileUploadIconOutdoorIntSmall.HasFile)
                {
                    temp.OutdoorIntSmall = Session["UserID"].ToString() + "Outdoor_Int_small.png";
                    FileUploadIconOutdoorIntSmall.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorIntSmall);
                }
                else
                    temp.OutdoorIntSmall = "Outdoor_Int_small.png";
                if (FileUploadIconOutdoorSmall.HasFile)
                {
                    temp.OutdoorSmall = Session["UserID"].ToString() + "Outdoor_Small.png";
                    FileUploadIconOutdoorSmall.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorSmall);
                }
                else
                    temp.OutdoorSmall = "Outdoor_Small.png";
                if (FileUploadIconPressBig.HasFile)
                {
                    temp.PressBig = Session["UserID"].ToString() + "Press_Big.png";
                    FileUploadIconPressBig.SaveAs(Server.MapPath("~/Templates/") + temp.PressBig);
                }
                else
                    temp.PressBig = "Press_Big.png";
                if (FileUploadIconPressSmall.HasFile)
                {
                    temp.PressSmall = Session["Userid"].ToString() + "Press_small.png";
                    FileUploadIconPressSmall.SaveAs(Server.MapPath("~/Templates/") + temp.PressSmall);
                }
                else
                    temp.PressSmall = "Press_small.png";
                if (FileUploadIconRadioBig.HasFile)
                {
                    temp.RadioBig = Session["UserID"].ToString() + "Radio_Big.png";
                    FileUploadIconRadioBig.SaveAs(Server.MapPath("~/Templates/") + temp.RadioBig);
                }
                else
                    temp.RadioBig = "Radio_Big.png";
                if (FileUploadIconRadioIntBig.HasFile)
                {
                    temp.RadioIntBig = Session["UserID"].ToString() + "Radio_int_big.png";
                    FileUploadIconRadioIntBig.SaveAs(Server.MapPath("~/Templates/") + temp.RadioIntBig);
                }
                else
                    temp.RadioIntBig = "Radio_int_big.png";
                if (FileUploadIconRadioIntSmall.HasFile)
                {
                    temp.RadioIntSmall = Session["UserID"].ToString() + "Radio_int_small.png";
                    FileUploadIconRadioIntSmall.SaveAs(Server.MapPath("~/Templates/") + temp.RadioIntSmall);
                }
                else
                    temp.RadioIntSmall = "Radio_int_small.png";
                if (FileUploadIconRadioSmall.HasFile)
                {
                    temp.RadioSmall = Session["UserID"].ToString() + "Radio_small.png";
                    FileUploadIconRadioSmall.SaveAs(Server.MapPath("~/Templates/") + temp.RadioSmall);
                }
                else
                    temp.RadioSmall = "Radio_small.png";
                temp.UserID = Convert.ToInt32(Session["UserID"]);
                temp.Slide1 = filename1;
                temp.Slide2 = filename2;
                temp.Slide3 = filename3;
                temp.Slide4 = filename4;
                temp.Slide5 = filename5;
                temp.Slide6 = filename6;
                temp.Save();
                BindData();
                lblSuccess.Text = "Uploaded Successfully";
            }
            else
            {
                if (fleTemplate1.HasFile)
                {
                    filename1 = Session["UserID"].ToString() + "Template 1.ppt";
                    fleTemplate1.SaveAs(Server.MapPath("~/Templates/") + filename1);
                }
                else
                    filename1 = temp.Slide1;
                if (fleTemplate2.HasFile)
                {
                    filename2 = Session["UserID"].ToString() + "Template 2.ppt";
                    fleTemplate2.SaveAs(Server.MapPath("~/Templates/") + filename2);
                }
                else
                    filename2 = temp.Slide2;
                if (fleTemplate3.HasFile)
                {
                    filename3 = Session["UserID"].ToString() + "Template 3.ppt";
                    fleTemplate3.SaveAs(Server.MapPath("~/Templates/") + filename3);
                }
                else
                    filename3 = temp.Slide3;
                if (fleTemplate4.HasFile)
                {
                    filename4 = Session["UserID"].ToString() + "Template 4.ppt";
                    fleTemplate4.SaveAs(Server.MapPath("~/Templates/") + filename4);
                }
                else
                    filename4 = temp.Slide4;
                if (fleTemplate5.HasFile)
                {
                    filename5 = Session["UserID"].ToString() + "Template 5.ppt";
                    fleTemplate5.SaveAs(Server.MapPath("~/Templates/") + filename5);
                }
                else
                    filename5 = temp.Slide5;
                if (fleTemplate6.HasFile)
                {
                    filename6 = Session["UserID"].ToString() + "Template 6.ppt";
                    fleTemplate6.SaveAs(Server.MapPath("~/Templates/") + filename6);
                }
                else
                    filename6 = temp.Slide6;
                if (FileUploadIconTVBig.HasFile)
                {
                    temp.TvBig = Session["UserID"].ToString() + "TV_Big.png";
                    FileUploadIconTVBig.SaveAs(Server.MapPath("~/Templates/") + temp.TvBig);
                }
               
                if (FileUploadIconTVSmall.HasFile)
                {
                    temp.TvSmall = Session["UserID"].ToString() + "TV_Small.png";
                    FileUploadIconTVSmall.SaveAs(Server.MapPath("~/Templates/") + temp.TvSmall);
                }
               
                if (FileUploadIconCableBig.HasFile)
                {
                    temp.CableBig = Session["UserID"].ToString() + "Cable_Big.png";
                    FileUploadIconCableBig.SaveAs(Server.MapPath("~/Templates/") + temp.CableBig);
                }
                
                if (FileUploadIconCableSmall.HasFile)
                {
                    temp.CableSmall = Session["UserID"].ToString() + "Cable_small.png";
                    FileUploadIconCableSmall.SaveAs(Server.MapPath("~/Templates/") + temp.CableSmall);
                }
                
                if (FileUploadIconInternetBig.HasFile)
                {
                    temp.InternetBig = Session["UserID"].ToString() + "Internet_big.png";
                    FileUploadIconInternetBig.SaveAs(Server.MapPath("~/Templates/") + temp.InternetBig);
                }
                
                if (FileUploadIconInternetSmall.HasFile)
                {
                    temp.InternetSmall = Session["UserID"].ToString() + "Internet_small.png";
                    FileUploadIconInternetSmall.SaveAs(Server.MapPath("~/Templates/") + temp.InternetSmall);
                }
               
                if (FileUploadIconOutdoorBig.HasFile)
                {
                    temp.OutdoorBig = Session["UserID"].ToString() + "Outdoor_Big.png";
                    FileUploadIconOutdoorBig.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorBig);
                }
                
                if (FileUploadIconOutdoorIntBig.HasFile)
                {
                    temp.OutdoorIntBig = Session["UserID"].ToString() + "Ootdoor_Int_Big.png";
                    FileUploadIconOutdoorIntBig.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorIntBig);
                }
                
                if (FileUploadIconOutdoorIntSmall.HasFile)
                {
                    temp.OutdoorIntSmall = Session["UserID"].ToString() + "Outdoor_Int_small.png";
                    FileUploadIconOutdoorIntSmall.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorIntSmall);
                }
                
                if (FileUploadIconOutdoorSmall.HasFile)
                {
                    temp.OutdoorSmall = Session["UserID"].ToString() + "Outdoor_Small.png";
                    FileUploadIconOutdoorSmall.SaveAs(Server.MapPath("~/Templates/") + temp.OutdoorSmall);
                }
                
                if (FileUploadIconPressBig.HasFile)
                {
                    temp.PressBig = Session["UserID"].ToString() + "Press_Big.png";
                    FileUploadIconPressBig.SaveAs(Server.MapPath("~/Templates/") + temp.PressBig);
                }
                
                if (FileUploadIconPressSmall.HasFile)
                {
                    temp.PressSmall = Session["Userid"].ToString() + "Press_small.png";
                    FileUploadIconPressSmall.SaveAs(Server.MapPath("~/Templates/") + temp.PressSmall);
                }
               
                if (FileUploadIconRadioBig.HasFile)
                {
                    temp.RadioBig = Session["UserID"].ToString() + "Radio_Big.png";
                    FileUploadIconRadioBig.SaveAs(Server.MapPath("~/Templates/") + temp.RadioBig);
                }
               
                if (FileUploadIconRadioIntBig.HasFile)
                {
                    temp.RadioIntBig = Session["UserID"].ToString() + "Radio_int_big.png";
                    FileUploadIconRadioIntBig.SaveAs(Server.MapPath("~/Templates/") + temp.RadioIntBig);
                }
               
                if (FileUploadIconRadioIntSmall.HasFile)
                {
                    temp.RadioIntSmall = Session["UserID"].ToString() + "Radio_int_small.png";
                    FileUploadIconRadioIntSmall.SaveAs(Server.MapPath("~/Templates/") + temp.RadioIntSmall);
                }
               
                if (FileUploadIconRadioSmall.HasFile)
                {
                    temp.RadioSmall = Session["UserID"].ToString() + "Radio_small.png";
                    FileUploadIconRadioSmall.SaveAs(Server.MapPath("~/Templates/") + temp.RadioSmall);
                }
               

                SPs.Insertupdatetemplate(Convert.ToInt32(temp.Id),filename1,filename2,filename3,filename4,filename5,filename6,temp.TvBig,temp.TvSmall,temp.CableSmall,temp.CableBig,temp.RadioSmall,temp.RadioBig,temp.RadioIntSmall,temp.RadioIntBig,temp.OutdoorSmall,temp.OutdoorBig,temp.OutdoorIntSmall,temp.OutdoorIntBig,temp.InternetBig,temp.InternetSmall,temp.PressBig,temp.PressSmall).Execute();
                BindData();
                lblSuccess.Text = "Updated Successfully";
            }
        }
        public void BindData()
        {
            DataTable dt = SPs.GettemplatesforUsers(Convert.ToInt32(Session["UserID"])).GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                RequiredFieldValidator1.Enabled = false;
                RequiredFieldValidator2.Enabled = false;
                RequiredFieldValidator3.Enabled = false;
                RequiredFieldValidator4.Enabled = false;
                RequiredFieldValidator5.Enabled = false;
                RequiredFieldValidator6.Enabled = false;
                temp.Id = Convert.ToInt32(dt.Rows[0][0]);
                temp.Slide1 = dt.Rows[0][2].ToString();
                temp.Slide2 = dt.Rows[0][3].ToString();
                temp.Slide3 = dt.Rows[0][4].ToString();
                temp.Slide4 = dt.Rows[0][5].ToString();
                temp.Slide5 = dt.Rows[0][6].ToString();
                temp.Slide6 = dt.Rows[0][7].ToString();
                temp.TvSmall = dt.Rows[0][10].ToString();
                temp.TvBig = dt.Rows[0][11].ToString();
                temp.CableSmall = dt.Rows[0][12].ToString();
                temp.CableBig = dt.Rows[0][13].ToString();
                temp.RadioSmall = dt.Rows[0][14].ToString();
                temp.RadioBig = dt.Rows[0][15].ToString();
                temp.RadioIntSmall = dt.Rows[0][16].ToString();
                temp.RadioIntBig = dt.Rows[0][17].ToString();
                temp.OutdoorSmall = dt.Rows[0][18].ToString();
                temp.OutdoorBig = dt.Rows[0][19].ToString();
                temp.OutdoorIntSmall = dt.Rows[0][20].ToString();
                temp.OutdoorIntBig = dt.Rows[0][21].ToString();
                temp.InternetBig = dt.Rows[0][22].ToString();
                temp.InternetSmall = dt.Rows[0][23].ToString();
                temp.PressBig = dt.Rows[0][24].ToString();
                temp.PressSmall = dt.Rows[0][25].ToString();
            }
            else
                temp.Id = 0;
        }
    }
}